<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

   <div id="app"></div>

   <script src="../../js/react.development.js"></script>
   <script src="../../js/react-dom.development.js"></script>
   <script src="../../js/babel.min.js"></script>


    <script type="text/babel">
       
        // 自己状态 / 自动更新
        class Clock extends React.Component{
            constructor(props){
                console.log('constructor');
                super(props)
                //01 state 初始化状态=> 通过this 挂载到组件实例上
                this.state={
                    time:new Date().toLocaleTimeString()
                }
            }
            
            
            render(){
                console.log('render');
                return (
                    <div>
                        <h1>Clock</h1>
                        <h3>time:{this.state.time}</h3>
                        <button onClick={()=>{
                            ReactDOM.unmountComponentAtNode(document.getElementById('app'))
                        }}>remove component</button>
                    </div>
                )
            }
           
            // 第一次渲染完毕会执行componentDidMount
            componentDidMount(){
                console.log('componentDidMount');
                this.timerId = setInterval(()=>{
                    console.log('setInterval');
                    //改变状态=>时间=>重新渲染=>更新时间
                    // 作用：改变状态 ，触发render执行
                    this.setState({time:new Date().toLocaleTimeString()})
                },1000)
            }


            //组件卸载之前会被自动调用
            // 资源释放 & 清理垃圾
            componentWillUnmount(){
                console.log('componentWillUnmount');
                clearInterval(this.timerId)
            }
        }

        ReactDOM.render(<Clock />,document.getElementById('app'))

       


    </script>
</body>
</html>